﻿using System;
using RssReader.Feeds;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace RssReader
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    partial class ExtendedSplashScreen
    {
        internal Rect SplashImageRect; // Rect to store splash screen image coordinates.
        internal bool Dismissed = false; // Variable to track splash screen dismissal status.
        internal Frame RootFrame;

        private SplashScreen _splash; // Variable to hold the splash screen object.
       
        public ExtendedSplashScreen(SplashScreen splashscreen, bool loadState)
        {
            InitializeComponent();

            Window.Current.SizeChanged += ExtendedSplash_OnResize;

            _splash = splashscreen;

            if (_splash != null)
            {
                // Register an event handler to be executed when the splash screen has been dismissed.
                _splash.Dismissed += DismissedEventHandler;

                // Retrieve the window coordinates of the splash screen image.
                SplashImageRect = _splash.ImageLocation;
                PositionImage();
            }

            // Create a Frame to act as the navigation context 
            RootFrame = new Frame();

            // Restore the saved session state if necessary
            RestoreStateAsync(loadState);   
        }

        async void RestoreStateAsync(bool loadState)
        {
            //if (loadState)
            //    await SuspensionManager.RestoreAsync();

            try
            {
                var categoryHelper = new CategoryHelper();
                var allItems = await categoryHelper.Get_all_blog_posts_from_shown_categories();
                RootFrame.Navigate(typeof(ItemsPage), allItems);
                Window.Current.Content = RootFrame;
                Window.Current.Activate();
            }
            catch (Exception)
            {
                DisplayError();
            }

            // Normally you should start the time consuming task asynchronously here and 
            // dismiss the extended splash screen in the completed handler of that task
            // This sample dismisses extended splash screen  in the handler for "Learn More" button for demonstration
        }

        private async void DisplayError()
        {
            var error =
                new MessageDialog(
                    "Windows 8 RSS Reader cannot connect to internet, please check your connection and try again")
                    {
                        Title = "Internet Connection Problem"
                    };
            await error.ShowAsync();
        }

        // Include code to be executed when the system has transitioned from the splash screen to the extended splash screen (application's first view).
        void DismissedEventHandler(SplashScreen sender, object e)
        {
            Dismissed = true;

            // Navigate away from the app's extended splash screen after completing setup operations here...
            // This sample navigates away from the extended splash screen when the "Learn More" button is clicked.
        }

        void ExtendedSplash_OnResize(Object sender, WindowSizeChangedEventArgs e)
        {
            // Safely update the extended splash screen image coordinates. This function will be fired in response to snapping, unsnapping, rotation, etc...
            if (_splash != null)
            {
                // Update the coordinates of the splash screen image.
                SplashImageRect = _splash.ImageLocation;
                PositionImage();
            }
        }

        void PositionImage()
        {
            ExtendedSplashImage.SetValue(Canvas.LeftProperty, SplashImageRect.X);
            ExtendedSplashImage.SetValue(Canvas.TopProperty, SplashImageRect.Y);
            ExtendedSplashImage.Height = SplashImageRect.Height;
            ExtendedSplashImage.Width = SplashImageRect.Width;
            
            ProgressRing.SetValue(Canvas.LeftProperty, SplashImageRect.X + (ExtendedSplashImage.Width / 2.5));
            ProgressRing.SetValue(Canvas.TopProperty, SplashImageRect.Y + ExtendedSplashImage.Height);
        }
    }
}
